<?php
require_once 'Database.class.php';

/**
 * @desc: Class shared video
 */

class Shared extends Database {
	function __construct() {
		parent::__construct();
		parent::connect();
	}

	/**
	 *
	 * Enter description here ...
	 * @param unknown_type $userID
	 * @param unknown_type $videoId
	 */
	public function checkShared($videoId = NULL, $userId = NULL) {
		$query = 'SELECT *
				 FROM shared s
				 WHERE s.video_id = "'.$videoId.'"
						AND s.user_id = "'.$userId.'"';
		$result = $this->fetchTable($query);
		if(count($result))
			return TRUE;
		else
			return FALSE;
	}

	public function insertShared($videoId = NULL, $userId = NULL, $shareIncrease = 0){
		$query = 'INSERT  INTO shared(video_id, user_id, `count`, shared_flg, date_created, date_updated)
						VALUES("'.$videoId.'", "'.$userId.'", '.$shareIncrease.', 1, SYSDATE(), SYSDATE())';

			return $this->executeQuery($query);
	}

	public function updateShareCount($videoId = NULL, $userId = NULL, $likeValue){
		$query = 'UPDATE  liked
					SET like_flg = '.$likeValue.'
					WHERE liked.video_id = "'.$videoId.'"
							AND liked.user_id = "'.$userId.'"';
			$result = $this->executeQuery($query);
	}

	public function shareCounter($userId = null){
		$query = 'SELECT SUM(`count`) count_total
				FROM shared s
				WHERE	s.user_id = "'.$userId.'"
					AND	s.count >0';

		$result = $this->fetchTable($query, 2);
		if($result){
			return $result[0]['count_total'];
		}
		else return 0;
	}

}